/**
 * 
 */
package it.sd.pwframework.persistence.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;


public class PaginatedResults<E> implements List<E>{
	
	ArrayList<E> results = new ArrayList<E>();

	/**
	 * 
	 */
	private Integer currentPage = null;

	private Long totalResults = null;

	private Integer itemXpage = null;

	public PaginatedResults(Pagination pagination){
		super();
		if (pagination!= null){
			this.itemXpage=pagination.getItemXpage();
			this.currentPage=pagination.getPage();
		}else{
			this.currentPage=0;
		}
	}

	/**
	 * @return the hasNext
	 */
	public boolean hasNext() {
		return (getCurrentPage()+1)<getTotalPages();
	}

	/**
	 * @return the currentPage
	 */
	public Integer getCurrentPage() {
		return currentPage;
	}

	/**
	 * @return the totalPages
	 */
	public Integer getTotalPages() {
		return itemXpage != null ? (int)((totalResults - 1) / itemXpage) + 1 : 1;
	}

	/**
	 * @return the totalResults
	 */
	public Long getTotalResults() {
		return totalResults;
	}

	/**
	 * @param totalResults
	 *            the totalResults to set
	 */
	public void setTotalResults(Long totalResults) {
		this.totalResults = totalResults;
	}

	/**
	 * @return the itemXpage
	 */
	public Integer getItemXpage() {
		return itemXpage;
	}

	public boolean add(E e) {
		return results.add(e);
	}

	public void add(int index, E element) {
		results.add(index, element);
	}

	public boolean addAll(Collection<? extends E> c) {
		return results.addAll(c);
	}

	public boolean addAll(int index, Collection<? extends E> c) {
		return results.addAll(index, c);
	}

	public void clear() {
		results.clear();
		
	}

	public boolean contains(Object o) {
		return results.contains(o);
	}

	public boolean containsAll(Collection<?> c) {
		return results.containsAll(c);
	}

	public E get(int index) {
		return results.get(index);
	}

	public int indexOf(Object o) {
		return results.indexOf(o);
	}

	public boolean isEmpty() {
		return results.isEmpty();
	}

	public Iterator<E> iterator() {
		return results.iterator();
	}

	public int lastIndexOf(Object o) {
		return results.lastIndexOf(o);
	}

	public ListIterator<E> listIterator() {
		return results.listIterator();
	}

	public ListIterator<E> listIterator(int index) {
		return results.listIterator(index);
	}

	public boolean remove(Object o) {
		return results.remove(o);
	}

	public E remove(int index) {
		return results.remove(index);
	}

	public boolean removeAll(Collection<?> c) {
		return results.removeAll(c);
	}

	public boolean retainAll(Collection<?> c) {
		return false;
	}

	public E set(int index, E element) {
		return results.set(index, element);
	}

	public int size() {
		return results.size();
	}

	public List<E> subList(int fromIndex, int toIndex) {
		return results.subList(fromIndex, toIndex);
	}

	public Object[] toArray() {
		return results.toArray();
	}

	public <T> T[] toArray(T[] a) {
		return results.toArray(a);
	}

	/**
	 * @return the results
	 */
	public ArrayList<E> getResults() {
		return results;
	}
}
